package com.srwl.mytx.widget;

import android.app.AlertDialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.RelativeLayout;

import com.srwl.mytx.Constant;
import com.srwl.mytx.activity.LoginActivity;
import com.srwl.mytx.domain.Contact;
import com.srwl.mytx.Helper;
import com.srwl.mytx.R;
import com.srwl.mytx.domain.User;
import com.srwl.mytx.parse.UserProfileManager;
import com.srwl.mytx.utils.Utils;

/**
 * Created by dahai2070 on 2018/5/18 0018.
 */

public class AuthorityDialog extends AlertDialog {
    private Context context;
    private Contact contact;
    private EditText editInfiniteKey;
    private EditText editInfiniteTag;
    private CheckBox checkBoxLocation;
    private CheckBox checkBoxTrace;
    private CheckBox checkBoxRelation;
    private View tv_tag;
    private CheckBox checkBoxCustomInfinite;
    private CheckBox checkBoxInfiniteChat;
    private CheckBox checkBoxlimteChat;
    private CheckBox checkBoxAdjustStructure;
    private View ll_CustomInfinite;
    private RelativeLayout rl_confirm;
    private RelativeLayout rl_cancel;
    private InteractionListener listener;

    public AuthorityDialog(Context context, Contact contact) {
        super(context);
        this.context = context;
        this.contact = contact;
    }

    public AuthorityDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    public AuthorityDialog(Context context, int themeResId) {
        super(context, themeResId);

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
        setContentView(R.layout.authority_dialog);
        editInfiniteTag = findViewById(R.id.et_infinite_tag);
        checkBoxLocation = findViewById(R.id.authority_location);
        checkBoxTrace = findViewById(R.id.authority_trace);
        checkBoxRelation = findViewById(R.id.relation_infinite);
        tv_tag = findViewById(R.id.tv_tag);
        checkBoxRelation.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                if (isChecked) {
                    tv_tag.setVisibility(View.VISIBLE);
                    editInfiniteTag.setVisibility(View.VISIBLE);
                } else {
                    tv_tag.setVisibility(View.INVISIBLE);
                    editInfiniteTag.setVisibility(View.INVISIBLE);
                }
            }
        });


        checkBoxCustomInfinite = findViewById(R.id.custom_infinite);
        checkBoxInfiniteChat = findViewById(R.id.infinite_chat);
        checkBoxlimteChat = findViewById(R.id.limit_chat);
        checkBoxAdjustStructure = findViewById(R.id.adjust_structure);
        ll_CustomInfinite = findViewById(R.id.ll_custom_infinite);
        editInfiniteKey = findViewById(R.id.et_infinite_key);
        rl_confirm = findViewById(R.id.rl_confirm);
        rl_cancel = findViewById(R.id.rl_cancel);
        rl_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        User loginUserInfo = UserProfileManager.getInstance().getLoginUserInfo();
        rl_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (Utils.isTempUser(loginUserInfo)) {
                    LoginActivity.startActivity(context, "设置权限前请先登录");
                    return;
                }

                if (listener != null) {
                    listener.onConfirm(getupdateInfo());
                }
            }
        });
        if (contact != null) {
            String loginUserId = loginUserInfo.getuId();
            //判断当前登录用户是否为主动添加方
            if (TextUtils.equals(loginUserId, contact.getmId())) {
                //如果为主动添加方,使用Tlocation判断我是否给对方权限
                if (TextUtils.equals(contact.getTLocation(), Constant.CONTACT_HAVE_AUTHORITY)) {
                    checkBoxLocation.setChecked(true);
                }
            } else {
                //如果为被动添加方,使用location判断我是否给对方权限
                if (TextUtils.equals(contact.getLocation(), Constant.CONTACT_HAVE_AUTHORITY)) {
                    checkBoxLocation.setChecked(true);
                }
            }

            if (TextUtils.equals(contact.getTTrace(), Constant.CONTACT_HAVE_AUTHORITY)) {
                checkBoxTrace.setChecked(true);
            }

            //判断当前登录的用户是否为主动添加方
            if (TextUtils.equals(loginUserId, contact.getmId())) {
                //如果为主动添加方,使用tRelationInfineite我是否给对方权限
                if (TextUtils.equals(contact.getTRelationInfinite(), Constant.CONTACT_HAVE_AUTHORITY)) {
                    checkBoxRelation.setChecked(true);
                }
            } else {
                //如果为被动添加方,使用relateionInfineite判断我是否给对方权限
                if (TextUtils.equals(contact.getRelationInfinite(), Constant.CONTACT_HAVE_AUTHORITY)) {
                    checkBoxRelation.setChecked(true);
                }
            }

            if (!TextUtils.isEmpty(contact.getInfiniteTag())) {
                editInfiniteTag.setText(contact.getInfiniteTag());
            }

            if (!TextUtils.isEmpty(contact.getInfiniteKey())) {
                editInfiniteKey.setText(contact.getInfiniteKey());
            }
            if (!TextUtils.isEmpty(contact.getInfiniteKey())) {
                if (TextUtils.equals(contact.getInfiniteChat(), Constant.CONTACT_HAVE_AUTHORITY)) {
                    checkBoxInfiniteChat.setChecked(true);
                }
                if (TextUtils.equals(contact.getUpChat(), Constant.CONTACT_HAVE_AUTHORITY)) {
                    checkBoxlimteChat.setChecked(true);
                }
                if (TextUtils.equals(contact.getAdjustStructure(), Constant.CONTACT_HAVE_AUTHORITY)) {
                    checkBoxAdjustStructure.setChecked(true);
                }
            }
        }

    }

    public void setListener(InteractionListener listener) {
        this.listener = listener;
    }

    public interface InteractionListener {

        void onConfirm(Contact updateContact);


    }


    public Contact getupdateInfo() {
        String loginUserId = UserProfileManager.getInstance().getLoginUserInfo().getuId();
        Contact updateContact = new Contact();
        //判断当前登录的用户是否为主动添加方
        if (TextUtils.equals(loginUserId, contact.getmId())) {
            //如果为主动添加方,那么判断之前tLocation的状态,并根据判断的结果,对tLocation设置为对应的状态
            if (checkBoxLocation.isChecked() && TextUtils.equals(contact.getTLocation(), Constant.CONTACT_NO_AUTHORITY)) {
                updateContact.setTLocation(Constant.CONTACT_HAVE_AUTHORITY);
            } else if (checkBoxLocation.isChecked() && TextUtils.equals(contact.getTLocation(), Constant.CONTACT_HAVE_AUTHORITY)) {
                updateContact.setTLocation(Constant.CONTACT_HAVE_AUTHORITY);
            } else if (!checkBoxLocation.isChecked() && TextUtils.equals(contact.getTLocation(), Constant.CONTACT_HAVE_AUTHORITY)) {
                updateContact.setTLocation(Constant.CONTACT_NO_AUTHORITY);
            } else if (!checkBoxLocation.isChecked() && TextUtils.equals(contact.getTLocation(), Constant.CONTACT_NO_AUTHORITY)) {
                updateContact.setTLocation(Constant.CONTACT_NO_AUTHORITY);
            }
        } else {
            //如果为被动添加方,那么判断location的状态,并根据判断的结果,对location设置为对应的状态
            if (checkBoxLocation.isChecked() && TextUtils.equals(contact.getLocation(), Constant.CONTACT_NO_AUTHORITY)) {
                updateContact.setLocation(Constant.CONTACT_HAVE_AUTHORITY);
            } else if (checkBoxLocation.isChecked() && TextUtils.equals(contact.getLocation(), Constant.CONTACT_HAVE_AUTHORITY)) {
                updateContact.setLocation(Constant.CONTACT_HAVE_AUTHORITY);
            } else if (!checkBoxLocation.isChecked() && TextUtils.equals(contact.getLocation(), Constant.CONTACT_HAVE_AUTHORITY)) {
                updateContact.setLocation(Constant.CONTACT_NO_AUTHORITY);
            } else if (!checkBoxLocation.isChecked() && TextUtils.equals(contact.getLocation(), Constant.CONTACT_NO_AUTHORITY)) {
                updateContact.setLocation(Constant.CONTACT_NO_AUTHORITY);
            }
        }
        if (checkBoxTrace.isChecked() && TextUtils.equals(contact.getTTrace(), Constant.CONTACT_NO_AUTHORITY)) {
            updateContact.setTTrace(Constant.CONTACT_HAVE_AUTHORITY);
        } else if (!checkBoxTrace.isChecked() && TextUtils.equals(contact.getTTrace(), Constant.CONTACT_HAVE_AUTHORITY)) {
            updateContact.setTTrace(Constant.CONTACT_NO_AUTHORITY);
        }

        //判断当前登录的是否为主动添加方
        if (TextUtils.equals(loginUserId, contact.getmId())) {
            //如果当前登录的是主动添加方,那么判断tRelationInfineite状态,并根据判断的结果,对tRelationInfineite设置对应的状态
            if (checkBoxRelation.isChecked() && TextUtils.equals(contact.getTRelationInfinite(), Constant.CONTACT_NO_AUTHORITY)) {
                updateContact.setTRelationInfinite(Constant.CONTACT_HAVE_AUTHORITY);
            } else if (checkBoxRelation.isChecked() && TextUtils.equals(contact.getTRelationInfinite(), Constant.CONTACT_HAVE_AUTHORITY)) {
                updateContact.setTRelationInfinite(Constant.CONTACT_HAVE_AUTHORITY);
            } else if (!checkBoxRelation.isChecked() && TextUtils.equals(contact.getTRelationInfinite(), Constant.CONTACT_HAVE_AUTHORITY)) {
                updateContact.setTRelationInfinite(Constant.CONTACT_NO_AUTHORITY);
            } else if (!checkBoxRelation.isChecked() && TextUtils.equals(contact.getTRelationInfinite(), Constant.CONTACT_NO_AUTHORITY)) {
                updateContact.setTRelationInfinite(Constant.CONTACT_NO_AUTHORITY);
            }
        } else {
            //如果当前登录的是被动添加方,那么判断relationInfinite状态,并根据判断的结果,对relationInfineite设置为对应的状态
            if (checkBoxRelation.isChecked() && TextUtils.equals(contact.getRelationInfinite(), Constant.CONTACT_NO_AUTHORITY)) {
                updateContact.setRelationInfinite(Constant.CONTACT_HAVE_AUTHORITY);
            } else if (checkBoxRelation.isChecked() && TextUtils.equals(contact.getRelationInfinite(), Constant.CONTACT_HAVE_AUTHORITY)) {
                updateContact.setRelationInfinite(Constant.CONTACT_HAVE_AUTHORITY);
            } else if (!checkBoxRelation.isChecked() && TextUtils.equals(contact.getRelationInfinite(), Constant.CONTACT_HAVE_AUTHORITY)) {
                updateContact.setRelationInfinite(Constant.CONTACT_NO_AUTHORITY);
            } else if (!checkBoxRelation.isChecked() && TextUtils.equals(contact.getRelationInfinite(), Constant.CONTACT_NO_AUTHORITY)) {
                updateContact.setRelationInfinite(Constant.CONTACT_NO_AUTHORITY);
            }
        }

        if (checkBoxAdjustStructure.isChecked() && TextUtils.equals(contact.getAdjustStructure(), Constant.CONTACT_NO_AUTHORITY)) {
            updateContact.setAdjustStructure(Constant.CONTACT_HAVE_AUTHORITY);
        } else if (!checkBoxAdjustStructure.isChecked() && TextUtils.equals(contact.getAdjustStructure(), Constant.CONTACT_HAVE_AUTHORITY)) {
            updateContact.setAdjustStructure(Constant.CONTACT_NO_AUTHORITY);
        }
        if (checkBoxlimteChat.isChecked() && TextUtils.equals(contact.getUpChat(), Constant.CONTACT_NO_AUTHORITY)) {
            updateContact.setUpChat(Constant.CONTACT_HAVE_AUTHORITY);
        } else if (!checkBoxlimteChat.isChecked() && TextUtils.equals(contact.getUpChat(), Constant.CONTACT_HAVE_AUTHORITY)) {
            updateContact.setUpChat(Constant.CONTACT_NO_AUTHORITY);
        }
        if (checkBoxInfiniteChat.isChecked() && TextUtils.equals(contact.getInfiniteChat(), Constant.CONTACT_NO_AUTHORITY)) {
            updateContact.setInfiniteChat(Constant.CONTACT_HAVE_AUTHORITY);
        } else if (!checkBoxInfiniteChat.isChecked() && TextUtils.equals(contact.getInfiniteChat(), Constant.CONTACT_HAVE_AUTHORITY)) {
            updateContact.setInfiniteChat(Constant.CONTACT_NO_AUTHORITY);
        }
        String key = editInfiniteKey.getText().toString().trim();
        String tag = editInfiniteTag.getText().toString().trim();
        if (!TextUtils.equals(contact.getInfiniteKey(), key)) {
            updateContact.setInfiniteKey(key);
        }
        if (!TextUtils.equals(contact.getInfiniteTag(), tag)) {
            updateContact.setInfiniteTag(tag);
        }
        return updateContact;
    }


}
